package adm.controlador;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.*;

import android.os.AsyncTask;
import android.util.Log;

public class Controller {
	
	private static final String URL_DB = null;
	//URL_DB="http://admhiking.zz.mu/usedb.php?lanzaquery="+query+"&t="+tipo

	public static JSONArray ConsultaBD(String consulta){
		String tipo="0";
		JSONArray jArray = null;
		ArrayList<NameValuePair> tupla = new ArrayList<NameValuePair>();
	    tupla.add(new BasicNameValuePair("lanzaquery",consulta));
	    tupla.add(new BasicNameValuePair("t",tipo));
	    InputStream is = null;
	    String result = null;
	    try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://admhiking.zz.mu/usedb.php");
            httpPost.setEntity(new UrlEncodedFormEntity(tupla));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            
            is = httpEntity.getContent();
 
        } 
	    catch (Exception e){
        	Log.e("HTTP", e.getMessage());
        }
		try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
		}
		catch(Exception e){
            Log.e("HTTP", e.getMessage());
		}
		try{
	        jArray = new JSONArray(result);
	  	}
		catch(JSONException e){
	        Log.e("JSON", "Error parsing data "+e.getMessage());
	    }
		
		return jArray;
 
				
	}

	public static boolean InsertaBD(String insert){
		String tipo="1";
		ArrayList<NameValuePair> tupla = new ArrayList<NameValuePair>();
	    tupla.add(new BasicNameValuePair("lanzaquery",insert));
	    tupla.add(new BasicNameValuePair("t",tipo));
	    InputStream is = null;
	    String result = null;
	    try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://admhiking.zz.mu/usedb.php");
            httpPost.setEntity(new UrlEncodedFormEntity(tupla));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
 
        } 
	    catch (Exception e){
	    	e.getMessage();
	    	e.printStackTrace();
	    	e.getCause();
	    	e.getLocalizedMessage();
        	Log.e("HTTP", e.getMessage());
        	return false;
        }
		
		return true;
	}
	
	public static boolean UpdateBD(String update){
		String tipo="1";
		ArrayList<NameValuePair> tupla = new ArrayList<NameValuePair>();
	    tupla.add(new BasicNameValuePair("lanzaquery",update));
	    tupla.add(new BasicNameValuePair("t", tipo));
	    InputStream is = null;
	    String result = null;
	    try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://admhiking.zz.mu/usedb.php?");
            httpPost.setEntity(new UrlEncodedFormEntity(tupla));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
 
        } 
	    catch (Exception e){
        	Log.e("HTTP", e.getMessage());
        	return false;
        }
		return true;
	}

	public static boolean BorraBD(String borrar){
		String tipo="1";
		ArrayList<NameValuePair> tupla = new ArrayList<NameValuePair>();
	    tupla.add(new BasicNameValuePair("lanzaquery",borrar));
	    tupla.add(new BasicNameValuePair("t", tipo));
	    InputStream is = null;
	    String result = null;
	    try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://admhiking.zz.mu/usedb.php?");
            httpPost.setEntity(new UrlEncodedFormEntity(tupla));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
 
        } 
	    catch (Exception e){
        	Log.e("HTTP", e.getMessage());
        	return false;
        }
		return true;
	}


}